﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Web.Routing;
using Microsoft.VisualStudio.DebuggerVisualizers;
using Sparrow.VisualStudio.Debugger.RouteVisualizer;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

[assembly: DebuggerVisualizer(typeof(Visualizer), typeof(Serializer), Target = typeof(RouteCollection), Description = "Mvc Route Visualizer")]
//[assembly: DebuggerVisualizer(typeof(Visualizer), typeof(Serializer), Target = typeof(RequestContext), Description = "Mvc Route Visualizer")]
namespace Sparrow.VisualStudio.Debugger.RouteVisualizer
{
    public class Visualizer : DialogDebuggerVisualizer
    {
        protected override void Show(IDialogVisualizerService windowService, IVisualizerObjectProvider objectProvider)
        {
            frmMain main = new frmMain();

            Stream stm = objectProvider.GetData();
            if (stm.Length != 0)
            {
                BinaryFormatter bf = new BinaryFormatter();
                var routes =
                    bf.Deserialize(stm) as RouteTransporterCollection;
                if (routes != null)
                {
                    foreach (var i in routes.Routes())
                    {
                        main.RouteCollection.Add(i);
                    }
                   
                }
                
                main.RequestContext = new DebugHttpContext(new DebugHttpRequest(routes.AppRelativeCurrentExecutionFilePath, routes.PathInfo));
            }

            main.ShowDialog();
        }

        public static void TestShowVisualizer(object objectToVisualize)
        {
            VisualizerDevelopmentHost visualizerHost = new VisualizerDevelopmentHost(objectToVisualize, typeof(Visualizer), typeof(Serializer));
            visualizerHost.ShowVisualizer();
        }
    }
}
